package com.example.rice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.rice.common.BaseContext;
import com.example.rice.common.R;
import com.example.rice.entity.Orders;
import com.example.rice.entity.User;
import com.example.rice.service.OrdersService;
import com.example.rice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;


/**
 * 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;


    /**
     * 用户下单方法
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 用户订单派送
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> ediPass(@RequestBody Orders orders){
        Orders orders1 = ordersService.getById(orders.getId());
        orders1.setStatus(orders.getStatus());
        ordersService.updateById(orders1);
        return R.success("订单状态发生改变");
    }

    /**
     * 查询个人信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getUser(int page, int pageSize){
        Page pageInfo = new Page<>(page,pageSize);
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        //分页查询
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 查询所有的订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getall(int page, int pageSize){
        Page pageInfo = new Page<>(page,pageSize);
        //分页查询
        ordersService.page(pageInfo);
        return R.success(pageInfo);
    }

}
